package com.foo.common.base.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.io.Files;

public class TomcatHelper {
	private final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TomcatHelper.class);

	public static String artifactId = "tenmalife-platform";
	private static String projectDirectory = "/Users/Steve/tools/myGit/";
	// private static String projectDirectory = "D:\\tools\\myGit\\";
	// private static String workingDirectory = "D:\\tmp\\tomcat8\\";
	private static String workingDirectory = "/Users/Steve/tmp/";
	private static String srcTomcatDir = workingDirectory + "apache-tomcat-8.0.36" + FooUtils.getFileSeparator();
	private static String targetTomcatDirName = workingDirectory + "tomcatUpgrade/apache-tomcat-8.0.36"
			+ FooUtils.getFileSeparator();;

	public static void main(String[] args) {

		try {
			copyOriginalTomcat();
			deleteUnnecessaryFiles();
			renameUnnecessaryFiles();
			copyNecessaryFiles();
			copyRootXmlOnTemplate();
			copyServerXmlOnTemplate();

		} catch (Exception e) {
			logger.error("Upgrade tomcat failure,error is:{}", e);
			FileUtils.deleteQuietly(new File(targetTomcatDirName));
		}

	}

	private static void copyServerXmlOnTemplate() throws IOException {
		logger.info("copyServerXmlOnTemplate of server.xml");

		Map<String, Object> bean = Maps.newHashMap();
		bean.put("server-port", 8005);
		bean.put("http-port", 8080);
		bean.put("http-redirectPort", 8443);
		bean.put("ajp-port", 8009);
		bean.put("ajp-redirectPort", 8443);
		bean.put("default-host-name", "127.0.0.1");
		bean.put("host-name", "127.0.0.1");

		MustacheFactory mf = new DefaultMustacheFactory();
		Mustache mustache = mf.compile("tomcatUpgrade/server.xml");
		Writer writer = new StringWriter();
		mustache.execute(writer, bean);
		writer.flush();
		String result = writer.toString();
		File file = new File(targetTomcatDirName + "conf" + FooUtils.getFileSeparator() + "server.xml");
		IOUtils.write(result, new FileOutputStream(file), "utf-8");
	}

	private static void copyRootXmlOnTemplate() throws IOException {
		logger.info("copyRootXmlOnTemplate of ROOT.xml");
		MustacheFactory mf = new DefaultMustacheFactory();
		Mustache mustache = mf.compile("tomcatUpgrade/ROOT.xml");
		Writer writer = new StringWriter();
		mustache.execute(writer,
				ImmutableMap.<String, Object> of("artifactId", artifactId, "projectDirectory", projectDirectory));
		writer.flush();
		String result = writer.toString();
		File rootFilePath = new File(targetTomcatDirName + "conf" + FooUtils.getFileSeparator() + "Catalina"
				+ FooUtils.getFileSeparator() + "127.0.0.1" + FooUtils.getFileSeparator() + "");
		FileUtils.forceMkdir(rootFilePath);
		IOUtils.write(result,
				new FileOutputStream(new File(rootFilePath + "" + FooUtils.getFileSeparator() + "ROOT.xml")), "utf-8");
	}

	private static void copyNecessaryFiles() throws IOException {
		logger.info("copyNecessaryFiles of jars");
		File libDir = new File(targetTomcatDirName + "lib");

		for (String tmp : ImmutableList.<String> of("tomcatUpgrade/logback-access-1.1.1.jar",
				"tomcatUpgrade/logback-core-1.1.1.jar", "tomcatUpgrade/mysql-connector-java-5.1.29.jar")) {
			FileUtils.copyFileToDirectory(FooUtils.getClassPathResourceFile(tmp), libDir);
		}

		logger.info("copyNecessaryFiles of conf");
		File confDir = new File(targetTomcatDirName + "conf");

		for (String tmp : ImmutableList.<String> of("tomcatUpgrade/tomcat-users.xml",
				"tomcatUpgrade/logging.properties", "tomcatUpgrade/logback-access.xml")) {
			FileUtils.copyFileToDirectory(FooUtils.getClassPathResourceFile(tmp), confDir);
		}

		logger.info("copyNecessaryFiles of .gitignore file for root folder.");
		File gitignoreDir = new File(targetTomcatDirName);
		for (String tmp : ImmutableList.<String> of("tomcatUpgrade/outer.gitignore")) {
			FileUtils.copyFileToDirectory(FooUtils.getClassPathResourceFile(tmp), gitignoreDir);
		}
		Files.move(new File(targetTomcatDirName + "outer.gitignore"), new File(targetTomcatDirName + ".gitignore"));

		logger.info("copyNecessaryFiles of .gitignore file for ROOT.xml ");
		String tmpDir = targetTomcatDirName + "conf" + FooUtils.getFileSeparator() + "Catalina"
				+ FooUtils.getFileSeparator() + "127.0.0.1" + FooUtils.getFileSeparator() + "";
		gitignoreDir = new File(tmpDir);
		for (String tmp : ImmutableList.<String> of("tomcatUpgrade/conf.gitignore")) {
			FileUtils.copyFileToDirectory(FooUtils.getClassPathResourceFile(tmp), gitignoreDir);
		}
		Files.move(new File(tmpDir + "conf.gitignore"), new File(tmpDir + ".gitignore"));

	}

	private static void copyOriginalTomcat() throws Exception {
		logger.info("copy whole project for artifact {} to directory:{}", artifactId, targetTomcatDirName);
		File tmpFile = new File(targetTomcatDirName);
		if (tmpFile.isDirectory()) {
			logger.warn("directory already exist,remove it now.");
			FileUtils.deleteDirectory(tmpFile);
		}
		FooUtils.copyDirectory(srcTomcatDir, targetTomcatDirName);
	}

	private static void deleteUnnecessaryFiles() throws IOException {
		logger.info("deleteUnnecessaryFiles now.");
		FileUtils.deleteDirectory(new File(targetTomcatDirName + "temp"));
		FileUtils.deleteDirectory(new File(targetTomcatDirName + "webapps" + FooUtils.getFileSeparator() + "docs"));
		FileUtils.deleteDirectory(new File(targetTomcatDirName + "webapps" + FooUtils.getFileSeparator() + "examples"));
		FileUtils.cleanDirectory(new File(targetTomcatDirName + "logs"));
		FileUtils.cleanDirectory(new File(targetTomcatDirName + "work"));
	}

	private static void renameUnnecessaryFiles() throws IOException {
		logger.info("renameUnnecessaryFiles now.");
		File hostManager = new File(targetTomcatDirName + "webapps" + FooUtils.getFileSeparator() + "host-manager");
		if (hostManager.isDirectory()) {
			Files.move(new File(targetTomcatDirName + "webapps" + FooUtils.getFileSeparator() + "host-manager"),
					new File(targetTomcatDirName + "webapps" + FooUtils.getFileSeparator()
							+ "feiYnn-console-host-manager"));
		}

		File manager = new File(targetTomcatDirName + "webapps" + FooUtils.getFileSeparator() + "manager");
		if (manager.isDirectory()) {
			Files.move(new File(targetTomcatDirName + "webapps" + FooUtils.getFileSeparator() + "manager"),
					new File(targetTomcatDirName + "webapps" + FooUtils.getFileSeparator() + "feiYnn-console-manager"));
		}

		File root = new File(targetTomcatDirName + "webapps" + FooUtils.getFileSeparator() + "ROOT");
		if (root.isDirectory()) {
			Files.move(new File(targetTomcatDirName + "webapps" + FooUtils.getFileSeparator() + "ROOT"),
					new File(targetTomcatDirName + "webapps" + FooUtils.getFileSeparator() + "feiYnn-console-config"));
		}

	}

	public void generateRootConfigFile() throws FileNotFoundException, IOException {
		Map<String, Object> scopes = Maps.newHashMap();
		scopes.put("artifactId", artifactId);
		scopes.put("projectDirectory", projectDirectory);
		if (artifactId.equals("industry-platform")) {
			scopes.put("common", false);
		} else {
			scopes.put("common", true);
		}
		MustacheFactory mf = new DefaultMustacheFactory();

		Mustache mustache = mf.compile("mustache-ROOT.xml");
		Writer writer = new StringWriter();
		mustache.execute(writer, scopes);
		writer.flush();
		String result = writer.toString();
		logger.info("result is:\n{}", result);
		// FooUtils.forceMkdir(FooUtils.global_auto_generated_directory_str);

		File resultFile = new File("C:\\Users\\think\\Desktop" + "\\root.xml");

		IOUtils.write(result, new FileOutputStream(resultFile), "utf-8");
		logger.info("generate file:{} on path:{}", resultFile.getName(), resultFile.getAbsolutePath());

	}

	public void generateDbTableSql() throws FileNotFoundException, IOException {
		logger.info("Create db sql is:{}",
				"CREATE DATABASE `" + artifactId + "` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci");
	}
}
